"""
cdn.views.admin

Views exposing the admin API in cdn.admin_api

NOTE: These views should be simple wrappers, because the admin API is accessible programmatically too.
"""
### Start of imports

# This line just better work...
from jackson import debug

# There's debug stuff for the rest
debug.pre_import(__name__)

debug.importing(__name__, 'admin_api from jackson.cdn')
from jackson.cdn import admin_api

debug.importing(__name__, 'HttpResponse from django.http', 2)
from django.http import HttpResponse

debug.importing(__name__, 'ext_json from jackson.extjson as json')
from jackson.extjson import ext_json as json

debug.importing(__name__, 'Server from jackson.chord')
from jackson.chord import Server

debug.importing(__name__, 'utils from jackson.chord as chord_utils')
from jackson.chord import utils as chord_utils

debug.importing(__name__, 'chord_api, core from jackson.cdn')
from jackson.cdn import chord_api, core

debug.importing(__name__, 'ContentEntry from jackson.cdn.models')
from jackson.cdn.models import ContentEntry

debug.post_import(__name__)

### End of imports

def dump_content_table_handler(request):
    context = [__name__, 'dump_content_table_handler()']
    debug.enter(context, None)
    entries = []
    debug.comment(context, 'Scanning ContentEntry table')
    for entry in ContentEntry.objects.all():
        list = [ entry.key, entry.filename, entry.mimetype, entry.url, entry.server, entry.status ]
        list = list + [ entry.md5_checksum, entry.urgency ]
        debug.comment(context, 'Scanning ' + str(list), 3)
        entries.append(list)
    response = HttpResponse(json.dumps(entries), mimetype='application/json')
    debug.leave(context, { 'entries' : 'To see these in debug, set VERBOSITY 5' })
    return response

def init_net_handler(request):
    context = [__name__, 'init_net_handler()']
    debug.enter(context, None, 2)
    b = admin_api.init_net()
    response = HttpResponse(json.dumps(b))
    debug.leave(context, None, 2)
    return response

def join_net_handler(request, ip = None, port = None):
    context = [__name__, 'join_net_handler()']
    debug.enter(context, None, 2)
    s = Server()
    s.ip = str(ip) # str() added because ip is unicode
    s.port = int(port)
    s.status = 'A'
    s.key = chord_utils.generate_server_key(ip, port)
    b = admin_api.join_net(s)
    response = HttpResponse(json.dumps(b))
    debug.leave(context, { 'b' : b }, 2)
    return response

def leave_net_handler(request):
    """ we've been told (through a browser) that we are leaving the ring.
    """
    context = [__name__, 'leave_net_handler()']
    debug.enter(context, None, 2)
    b = admin_api.leave_net()
    response = HttpResponse(json.dumps(b))
    debug.leave(context, { 'b' : b }, 2)
    return response

def whoami_handler(request):
    context = [__name__, 'whoami_handler()']
    debug.enter(context, None)
    me = chord_api.get_me()
    low = core.get_state('low')
    high = core.get_state('high')
    status = core.get_state('status')
    pkg = { 'me' : me, 'low' : low, 'high' : high, 'status' : status }
    response = HttpResponse(json.dumps(pkg))
    debug.leave(context, pkg)
    return response
